<?php

/**

 * 上传文件接口

 * @author maniac

 */

namespace app\api\controller\v1;



use app\facade\Upload AS UploadFacade;
use app\facade\Wechat as WechatFacade;
use think\image as Images;



class Upload extends Base
{
    /**
     * 上传文件接口
     */
    public function upload()

    {

        $data = input('param.');
        $data['path']='uploads';
        $data['type']='image';
        $data['module']='api';


        $data['path'] = isset($data['path']) && $data['path'] ? $data['path'] : 0;

        $data['module'] = !empty($data['module']) ? $data['path'] : 'api';
//        try {

            $result = UploadFacade::upload($data);

            if ($result) {

                return j(0, '上传成功', $result);

            }

            return j(1, '上传失败');

//        } catch (\Exception $e) {

//            return j(1, $e->getMessage());

//        }

    }

    /**
     * 附件管理
     */
    public function filemanager()

    {

        $data = input('param.');

        try {

            $result = UploadFacade::filemanager($data);

            if ($result) {

                return j(0, '获取附件成功', $result);

            }

            return j(1, '获取附件失败');

        } catch (\Exception $e) {

            return j(1, $e->getMessage());

        }

    }

    /*解析微信上的下载的图片*/
    //微信保存图片

    public function saveWximage(){

        $media_id=input("mid","");
        $access_token = WechatFacade::getAccessToken();
        $pic_url = "http://file.api.weixin.qq.com/cgi-bin/media/get?access_token={$access_token}&media_id={$media_id}";
        $path=$this->imgSave($pic_url);
        $pic_arr=[
            "attach_uri"=>$path,
            "module"=>'api',
            // "status"=>1,
            "create_time"=>time()
        ];
        $id=db("attaches")->insertGetId($pic_arr);
        $data=[
            "path"=>$path,
            "url"=>'http://'.$_SERVER['HTTP_HOST'].$path,
            "id"=>$id
        ];
        return j(0, '获取附件成功', $data);
    }
    /**

     * @WEB    http://blog.iinu.com.cn
     * @param  [Base64] $base64_image_content [要保存的Base64]
     * @param  [目录] $path [要保存的路径]
     */

    function imgSave($url){
        //$url="http://file.api.weixin.qq.com/cgi-bin/media/get?access_token=27_f0KSsXLXJdUSLTDA3_bYmrI0AREsDSqg3QeJENRKxnrYlhaFuR8RxG09nD3acMarsCmL3Yq7gC7tCXmBXsZkHmoYa1p1-Us8qOrgx2iG_UNeYbVkZ285e1RhEwuIorXx_iItxrT1NKCsvXbUPYDgAGAHQV&media_id=YKve5v5KoaX2oEggS3BMkQzvuL74nTc0DbOWPr_7PThEMHMY5A3TN1Fn73U5-mPu";
        file_put_contents('s1.txt',$url);
        $header = array(
            'User-Agent: Mozilla/5.0 (Windows NT 6.1; Win64; x64; rv:45.0) Gecko/20100101 Firefox/45.0',
            'Accept-Language: zh-CN,zh;q=0.8,en-US;q=0.5,en;q=0.3',
            'Accept-Encoding: gzip, deflate',);
        $curl = curl_init();
        curl_setopt($curl, CURLOPT_URL, $url);
        curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
        curl_setopt($curl, CURLOPT_FOLLOWLOCATION, true);
        curl_setopt($curl, CURLOPT_ENCODING, 'gzip');
        curl_setopt($curl, CURLOPT_HTTPHEADER, $header);
        $data = curl_exec($curl);
        $code = curl_getinfo($curl, CURLINFO_HTTP_CODE);
        curl_close($curl);
        if ($code == 200) {//把URL格式的图片转成base64_encode格式的！
            $path = '/static/uploads/api/image/1/'.date('Ymd', time()).'/'.md5(uniqid()).'.jpg';
            $new_file = $_SERVER["DOCUMENT_ROOT"].$path;
            $strSaveFileDir  = dirname($new_file);
            if (!file_exists($strSaveFileDir)) {
                mkdir($strSaveFileDir, 0777, true);
            }
            if (file_put_contents($new_file, $data) && file_exists($new_file)) {
                //$image = Images::open($new_file);
                //$image->tilewater('./logo.png',100)->save($new_file);
                return $path;
            }else{
                return "";
            }
        }
    }

}

